package com.iceland.client.service;

import com.iceland.client.client.cmd.CmdClient;
import com.iceland.client.client.proxy.ProxyClient;
import com.iceland.client.util.ClientChannelContext;
import com.iceland.common.message.MessageFactory;
import com.iceland.common.rpc.RemoteService;
import com.iceland.common.rpc.RpcMessageManager;
import com.iceland.common.rpc.RpcMessageManagerHolder;
import com.iceland.common.service.client.RemoteClientService;
import com.iceland.common.util.AttributeKeyUtils;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RemoteService(path = "client")
public class ClientServiceImpl implements RemoteClientService {


    @Override
    public void createDataExchangeChannel(Integer proxyServerPort) {
        log.info("为代理服务器{}创建流量转发通道",proxyServerPort);
        RpcMessageManager rpcMessageManager = RpcMessageManagerHolder.getRpcMessageManager();
        ClientChannelContext channelContext =(ClientChannelContext) rpcMessageManager.getChannelContext();
        CmdClient cmdClient = channelContext.getCmdClient();
        ProxyClient proxyClient = channelContext.getProxyClient();
        ChannelFuture channelFuture = cmdClient.connectDataExchangeServer(proxyServerPort);
        //通道还没建立好，关闭读
        channelFuture.channel().config().setAutoRead(false);

        ClientChannelContext clientChannelContext = new ClientChannelContext(rpcMessageManager,null,cmdClient,proxyClient);
        AttributeKeyUtils.setChannelContext(channelFuture.channel(),clientChannelContext);


        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if(future.isSuccess()){
                    /**
                     * 连接至真实服务器，并绑定通道
                     */
                    ChannelFuture proxyChannelFuture = proxyClient.connectServer();
                    ClientChannelContext proxyChannelContext = new ClientChannelContext(rpcMessageManager,channelFuture.channel(),cmdClient,proxyClient);
                    AttributeKeyUtils.setChannelContext(proxyChannelFuture.channel(),proxyChannelContext);

                    clientChannelContext.setBindChannel(proxyChannelFuture.channel());

                    proxyChannelFuture.addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            if(future.isSuccess()){
                                log.info("连接真实服务器成功，真实服务器：{}，映射的代理服务端口为：{}",proxyClient.getRemoteHost()+":"+proxyClient.getPort(),proxyServerPort);
                                //开启读
                                channelFuture.channel().config().setAutoRead(true);

                            }
                            else {
                                log.error("连接真实服务器失败，关闭相应通道,真实服务器：{}",proxyClient.getRemoteHost()+":"+proxyClient.getPort());
                                //关闭所有通道
                                channelFuture.channel().close();
                                future.channel().close();
                            }
                        }
                    });
                }
                else{
                    channelFuture.channel().close();
                }
            }
        });




    }
}
